package org.automated.realestatemanager.db.entity;

import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "publishers")
public class Publisher extends HasId implements Comparable<Publisher> {

	public Publisher() {
	}

	public Publisher(final Phone phone) {
		phones.add(phone);
	}

	public Publisher(final String name) {
		this.name = name;
	}

	@Id
	@Column(name = "publisher_id")
	@GeneratedValue(strategy = GenerationType.AUTO)
	public Long id;

	@Column(length = 50)
	private String name = "";

	@Column(length = 50)
	private String status = "";

	@Column(length = 50)
	private String source = "";

	@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
	@JoinTable(name = "publ_phones")
	private Set<Phone> phones = new TreeSet<Phone>();

	@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	@JoinTable(name = "publ_adv")
	private List<Advertisement> advertisements = new LinkedList<Advertisement>();

	@ManyToOne
	@JoinColumn(name = "agency_id")
	private Agency agency;

	public int compareTo(final Publisher o) {
		return id.compareTo(o.getId());
	}

	@Override
	public String toString() {
		return "Publisher [id=" + id + ", name=" + name + ", phones=" + phones
				+ "]";
	}

	@Override
	public Long getId() {
		return id;
	}

	@Override
	public void setId(final Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(final String name) {
		this.name = name;
	}

	public List<Advertisement> getAdvertisements() {
		return advertisements;
	}

	public void setAdvertisements(final List<Advertisement> advertisements) {
		this.advertisements = advertisements;
	}

	public Set<Phone> getPhones() {
		return phones;
	}

	public void setPhones(final Set<Phone> phones) {
		this.phones = phones;
	}

	public Agency getAgency() {
		return agency;
	}

	public void setAgency(final Agency agency) {
		this.agency = agency;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(final String status) {
		this.status = status;
	}

	public String getSource() {
		return source;
	}

	public void setSource(final String source) {
		this.source = source;
	}

}
